package hu.dynamicwebshop;

import com.vaadin.annotations.PreserveOnRefresh;
import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.CustomizedSystemMessages;
import com.vaadin.server.SystemMessages;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.UI;
import hu.dynamicwebshop.gui.AlignmentCenterLayer;
import hu.dynamicwebshop.gui.UserLayer;
import hu.dynamicwebshop.languagemanager.LanguageManager;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

@Theme("mytheme")
@SuppressWarnings("serial")
public class MyVaadinUI extends UI {

    private ApplicationContext ctx;
    private LanguageManager languageManager;
    private static final Logger log = Logger.getLogger(MyVaadinUI.class.getName());

    @PreserveOnRefresh
    @WebServlet(value = "/*", asyncSupported = true)
    //@VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "your.company.AppWidgetSet")
    @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class)
    public static class Servlet extends VaadinServlet {
    }

    //itt a program belepesi pontja
    @Override
    protected void init(VaadinRequest request) {

//        log.info("init(info)");
//        log.warn("init(warn)");
//        log.error("init(error)");
        ctx = WebApplicationContextUtils.
                getWebApplicationContext(VaadinServlet.getCurrent().getServletContext());

        AlignmentCenterLayer alignmentCenterLayer = (AlignmentCenterLayer) ctx.getBean("alignmentCenterLayer");
        setContent(alignmentCenterLayer);

        if (languageManager == null) {
            languageManager = (LanguageManager) ctx.getBean("languageManager");
            languageManager.setCountryCode(prepareCountryCode());
            languageManager.putTextsToOutputTextMap();
        }

        ((UserLayer) ctx.getBean("userLayer")).createUserLayer();
    }

    public String prepareCountryCode() {
        String locale = getCurrent().getLocale().toString();

        StringTokenizer tokenizer = new StringTokenizer(locale, "_");
        return tokenizer.nextElement().toString().toUpperCase();
    }

    public static SystemMessages getSystemMessages() {
        CustomizedSystemMessages systemMessage = new CustomizedSystemMessages();
        LanguageManager languageManager = new LanguageManager();

        if (getCurrent().getSession().getLocale().equals("HU")) {
            systemMessage.setSessionExpiredCaption("Inaktív volt 15 percig!");
            systemMessage.setSessionExpiredMessage("Kattintson ide az újratöltéshez, és jelentkezzen be újra.");
            systemMessage.setSessionExpiredNotificationEnabled(true);
            systemMessage.setSessionExpiredURL("http://localhost:8080/WebShop/");
            systemMessage.setCommunicationErrorCaption("Az oldal jelenleg nem elérhető!");
            systemMessage.setCommunicationErrorMessage("A hibáért elnézést kérünk, kérjük látogassa meg webáruházunkat később.");
            systemMessage.setCommunicationErrorNotificationEnabled(true);
        }
        return systemMessage;
    }
}
